trigger OORRestictDuplicateAssignmentGroup on Assignment_Group_Name__c (before Insert,before Update) {

   //Trigger Name: OORRestictDuplicateAssignmentGroup
   //Author: Accenture IDC
   //Project Name: OOR Phase 2   
   //Purpose: Restrict User from Saving Duplicate Assigment Group. 
   //Date: 24/01/2012
   
   
   Map<String,Assignment_Group_Name__c> assignGroupMap= New Map<String,Assignment_Group_Name__c>();//Map for Storing Assignment Group Name on Insert and Update(Only if the Type is Lead)

   for(Assignment_Group_Name__c assignGrp:Trigger.New){
      if(assignGrp.Type__c=='Lead' && (Trigger.ISINSERT ||(Trigger.ISUPDATE && assignGrp.Name!=Trigger.OldMap.get(assignGrp.Id).Name))){
         assignGroupMap.put(assignGrp.Name,assignGrp);
         }
       } 
     
      for(Assignment_Group_Name__c assignGrpName:[Select Id,Name from Assignment_Group_Name__c where Name IN:assignGroupMap.KeySet()]){
       
       Assignment_Group_Name__c assignGrp=assignGroupMap.get(assignGrpName.Name);
       assignGrp.Name.addError('Duplicate Assignment Group Name found.There is already a Assignment Group existing.');//Displaying error on the Detail Page.
       }      
    }